package com.trace.meat.controller;

import com.trace.meat.entity.Process;
import com.trace.meat.entity.Product;
import com.trace.meat.entity.Result;
import com.trace.meat.service.ProcessService;
import com.trace.meat.service.ProductService;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2021-06-19 11:17:29
 */
@RestController
@RequestMapping("product")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Resource
    private ProcessController processController;
    /**
     * 通过主键查询单条数据
     *
     * @param pid 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Product selectOne(Integer pid) {
        return this.productService.queryById(pid);
    }
    /**
     * 新增数据
     */
    //http://127.0.0.1:11451/product/insertOne?pid=1&type=5001001&weight=3.5&ptime=2016-12-12&lastOid=1&state=1
//    @GetMapping("insertOne")
//    public Product insert(Integer pid, Integer type, Double weight, String ptime,Integer lastOid,Short state) {
//        Product product=new Product();
//        product.setPid(pid);
//        product.setType(type);
//        product.setWeight(weight);
//        product.setPtime(ptime);
//        product.setLastOid(lastOid);
//        product.setState(state);
//        return this.productService.insert(product);
//    }
    /**
     * 更新数据
     *
     */
    //http://127.0.0.1:11451/product/updateOne?pid=1&bid=2
    @GetMapping("updateOne")
    public Product update(Integer pid, Integer type, Double weight, String ptime,Integer lastOid,Short state) {
        Product product=new Product();
        product.setPid(pid);
        product.setType(type);
        product.setWeight(weight);
        product.setPtime(ptime);
        product.setLastOid(lastOid);
        product.setState(state);
        return productService.update(product);
    }
    /**
     * 删除数据
     */
    //http://127.0.0.1:11451/product/deleteOne?pid=2
    @GetMapping("deleteOne")
    public boolean deleteById(Integer pid) {
        return productService.deleteById(pid);
    }
    /**
     * 插入新数据
     *
     * @param type 肉品类型
     * @param weight 重量
     * @param time 加工时间
     * @param lastoid 最新订单号
     * @param state 状态
     * @param bid 屠宰着id
     * @param aid 牲畜id
     * @return 单条数据
     */
    @GetMapping("insert")
    public Result insert(Integer type, Double weight, String time, Integer lastoid, short state,Integer bid,Integer aid) {
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        Product product=new Product();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(time);
        } catch (ParseException e) {
            System.out.println("时间输入格式错误");
            e.printStackTrace();
            result.setMsg("时间未按格式输入");
            return result;
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
        }
        product.setLastOid(lastoid);
        product.setPtime(time);
        product.setState(state);
        product.setType(type);
        product.setWeight(weight);
        product=productService.insert(product);
        this.processController.insert(bid,aid,product.getPid());
        result.setMsg("加工完成");
        result.setDetail(product);
        result.setSuccess(true);
        return result;
    }

}
